package com.splider.toutiao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.apache.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class CommonUtils {
	
	private static Logger logger = Logger.getLogger("CommonUtil");
	
	private static int words = 10000;

	public static JSONObject getUrlParam() {
		JSONObject jsonObject = null;
		FileReader reader = null;
		try {
			ScriptEngineManager manager = new ScriptEngineManager();
			ScriptEngine engine = manager.getEngineByName("javascript");
			
			String jsFileName = System.getProperty("user.dir")+File.separator+"toutiao.js"; // 读取js文件

			reader = new FileReader(jsFileName); // 执行指定脚本
			engine.eval(reader);

			if (engine instanceof Invocable) {
				Invocable invoke = (Invocable) engine;
				Object obj = invoke.invokeFunction("getParam");
				jsonObject = JSONObject.parseObject(obj != null ? obj.toString() : null);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return jsonObject;
	}
	
	public static String encodeHtml(String html){
		if(StringUtils.isEmpty(html)){
			return "";
		}
		html = html.replace("<", "&lt;");
		html = html.replace(">", "&gt;");
		html = html.replace("\"", "&quot;");
		html = html.replace(" ", "&nbsp;");
		html = html.replaceAll("[\t\n\t]", "");
		return html;
	}
	
	public static String decodeHtml(String html){
		if(StringUtils.isEmpty(html)){
			return "";
		}
		html = html.replace("&lt;","<");
		html = html.replace("&gt;",">");
		html = html.replace("&quot;","\"");
		html = html.replace("&nbsp;"," ");
		return html;
	}
	
	public static String simple2tw(String content){
		//1w个字符调用一次shell
		StringBuffer cs = new StringBuffer("");
		int length = content.length();
		int count = length/words;
		int ys = length%words;
		if (count==0) {
			cs.append(sc2tw(content));
		}else{
			for(int i=0;i<count;i++){
				cs.append(sc2tw(content.substring(i*words, (i+1)*words)));
				length = (i+1)*words;
			}
			if(ys>0){
				cs.append(sc2tw(content.substring(length)));
			}
		}
		return cs.toString();
	}
	
	public static Object sc2tw(String content){
		try {
			StringBuffer sb = new StringBuffer("");
			Process ps = Runtime.getRuntime().exec(ApplicationConfig.prop.getProperty("shell")+" \""+content+"\"");
			BufferedReader in = new BufferedReader(new InputStreamReader(ps.getInputStream()));
			String line = null;
			ps.waitFor();
			while((line = in.readLine())!=null){
				sb.append(line);
			}
			in.close();
			if(sb.indexOf("\"")==0){
				sb = sb.deleteCharAt(0);
			}
			if(sb.lastIndexOf("\"")==sb.length()-1){
				sb = sb.deleteCharAt(sb.length()-1);
			}
			ps.destroy();
			line = null;
			return sb;
		} catch (IOException e) {
//			logger.error("shell执行出错");
		} catch (InterruptedException e) {
//			logger.error("shell执行出错");
		}
		return content;
	}
	/**
	 * 抓取可用的代理IP
	 */
	public static List<ProxyIP> getProxyIPList(){
		List<ProxyIP> list = getKuaiDaiLi();
		if(list==null||list.size()==0){
			list = getFreeIPList();
		}
		if(list==null||list.size()==0){
			String[] urls = {ApplicationConfig.prop.getProperty("proxy_url")};
//			String[] urls = {"http://dev.kuaidaili.com/api/getproxy/?orderid=958462473108491&num=600&b_pcchrome=1&b_pcie=1&b_pcff=1&protocol=1&method=2&an_an=1&an_ha=1&quality=1&sort=1&sep=1"};
			list = getProxyIPs(urls[0]);
		}
		return list;
	}
	
	public static List<ProxyIP> getFreeIPList(){
		List<ProxyIP> ipList = new ArrayList<ProxyIP>();
		for (int page = 1; page <= 10; page++) {
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			Connection conn = Jsoup.connect("http://nyloner.cn/proxy?page="+page+"&num=15"+getTokenParam(page, 15)).ignoreContentType(true);
			Document doc = null;
			try {
				doc = conn.get();
			} catch (IOException e) {
				logger.error("fetch proxy ip error:"+e.getMessage());
			}
			if(doc!=null){
				String json = doc.body().text();
				JSONObject jsonObject = JSONObject.parseObject(json);
				JSONArray list = jsonObject.getJSONArray("list");
				if(list!=null&&list.size()>0){
					for (int i = 0; i < list.size(); i++) {
						try {
							ProxyIP ip = new ProxyIP();
							ip.setIp(list.getJSONObject(i).getString("ip"));
							ip.setPort(list.getJSONObject(i).getIntValue("port"));
							ipList.add(ip);
						} catch (Exception e) {
						}
					}
				}
			}
		}
		return ipList;
	}
	
	public static List<ProxyIP> getKuaiDaiLi(){
		List<ProxyIP> ipList = new ArrayList<ProxyIP>();
		for (int page = 1; page <= 10; page++) {
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			Connection conn = Jsoup.connect("http://www.kuaidaili.com/proxylist/"+page).ignoreContentType(true);
			Document doc = null;
			try {
				doc = conn.get();
			} catch (IOException e) {
				logger.error("fetch proxy ip error:"+e.getMessage());
			}
			if(doc!=null){
				Elements tbody = doc.getElementsByTag("tbody");
				if(tbody!=null&&tbody.size()>0){
					Elements trs = tbody.get(0).getElementsByTag("tr");
					if(trs!=null&&trs.size()>0){
						for (Element tr : trs) {
							Elements td = tr.getElementsByTag("td");
							if(td!=null&&td.size()>0){
								try {
									ProxyIP ip = new ProxyIP();
									ip.setIp(td.get(0).text().trim());
									ip.setPort(Integer.parseInt(td.get(1).text().trim()));
									ipList.add(ip);
								} catch (Exception e) {
								}
							}
						}
					}
				}
			}
		}
		return ipList;
	}
	
	public static List<ProxyIP> getProxyIPs(String url){
		List<ProxyIP> ipList = new ArrayList<ProxyIP>();
		Connection con = Jsoup.connect(url).timeout(10000)
				.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31");
		try {
			Document doc = con.get();
			Elements body = doc.getElementsByTag("body");
			if(body!=null&&body.size()>0){
				Element b = body.get(0);
				String ips = b.text();
				if(StringUtils.isNotEmpty(ips)&&!ips.toLowerCase().contains("error")){
					String[] iparray = ips.split(" ");
					for(String ip : iparray){
						ip = ip.replace("：", ":");
						ProxyIP proxyIP = new ProxyIP();
						proxyIP.setIp(ip.split(":")[0]);
						proxyIP.setPort(Integer.parseInt(ip.split(":")[1]));
						ipList.add(proxyIP);
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return ipList;
	}
	
	public static void main(String[] args) {
		List<ProxyIP> list = getKuaiDaiLi();
		System.out.println(list.size());
	}
	
	public static String getTokenParam(int page,int num){
		int timestamp = (int) (System.currentTimeMillis()/1000);
		return "&token="+MD5Util.MD5Str(page+""+num+timestamp)+"&t="+timestamp;
	}
}
